﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ExplodingCow.Basilisk.Exceptions.Logging
{
    /// <summary>
    /// Custom Exception Handling Logger for project Basalisk
    /// </summary>
    public class BasaliskExceptionWriter
    {
        /// <summary>
        /// Singletion instance 
        /// </summary>
        private static BasaliskExceptionWriter instance;
        /// <summary>
        /// the current place the log is located and written to
        /// </summary>
        private string logFilePath;
        /// <summary>
        /// private constructor for singleton
        /// </summary>
        /// <param name="logFilePathToUse">the path the current log file</param>
        private BasaliskExceptionWriter(string logFilePathToUse)
        {
            this.logFilePath = logFilePathToUse;
        }
        /// <summary>
        /// Method that Gets you the only instance of this Class
        /// </summary>
        /// <param name="logFilePathToUse">the path where the log exists</param>
        /// <returns>The only instance of this Class</returns>
        public static BasaliskExceptionWriter GetExceptionWriter(string logFilePathToUse)
        {
            if (instance == null)
            {
                instance = new BasaliskExceptionWriter(logFilePathToUse);
            }
            return instance;
        }
        /// <summary>
        /// private: checks to see if the log file exists
        /// </summary>
        /// <param name="logFilePathToUse">the current log file path</param>
        /// <returns>a boolean value whether it exists</returns>
        private bool verifyFileExists(string logFilePathToUse)
        {
            //check that the File Exists
            //return whether it does or not
            bool exists = false;
            return exists;
        }
        /// <summary>
        /// private: create a new logfile
        /// </summary>
        /// <param name="logFilePathToUse">the file path to create the log file</param>
        private void createNewFile(string logFilePathToUse)
        {

        }
        /// <summary>
        /// private: contains logic for logging the exception and the format for writing it
        /// </summary>
        /// <param name="basaliskException">The Current Custom Exception type</param>
        private void writeExceptionData(BasaliskException basaliskException)
        {
            string date;
            string innerException;
            string moduleName;
            string assemblyLocation;
            string methodName;

            //break out the logic for this
            FileStream fs = new FileStream(logFilePath, FileMode.Append);
            //this point will only be hit, if the directory or file already exist
            fs = File.OpenWrite(this.logFilePath);

            StreamWriter sw = new StreamWriter(fs);
            sw.Write(false);
            //log the time
            DateTime now = DateTime.Now;
            date = now.Date.ToString();
            //the module
            assemblyLocation = basaliskException.TargetSite.Module.Assembly.CodeBase;
            moduleName = basaliskException.TargetSite.Module.Name;
            methodName = basaliskException.TargetSite.ToString();
            //the exception
            innerException = basaliskException.InnerException.GetType().ToString();
           
            //other helpful data if available
        }
        /// <summary>
        /// Log the current exception data to the ExceptionWriter's logfile
        /// </summary>
        /// <param name="basaliskException">Custom Exception Type for Basalisk</param>
        public void LogException(BasaliskException basaliskException)
        {
            bool fileExists = false;
            fileExists = verifyFileExists(logFilePath);
            //exists, so write the exception to logFile
            if (fileExists)
            {
                //write Basalisk exception data to the log file
                writeExceptionData(basaliskException);
            }
            //otherwise createTheFile in the specified directory
            else
            {
                createNewFile(logFilePath);
            }

            //write the exception information to the log file
        }


    }
}
